<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('文件列表')" />
</head>
<style>
/*     .layui-inline{margin-right: 20px !important;} */
.layui-table-tool-temp{width:100%;padding-right:0;}
.layui-btn-disabled p {color:#C9C9C9 !important;}
.max-warn{float:right;}
.file-view{background: cornflowerblue;}
</style>
<body>
<form>
<div style="padding: 0 10px;">
	<div class="south">
		<div class="left">
			<div style="float:left"><table class="layui-hide" id="dhtable" lay-filter="ftable"></table></div>
		</div>
	</div>
</div>
</form>
<th:block th:include="include :: footer" />
<script type="text/html" id="toolbar">
	<div class="layui-btn-container" th:if="( ${buttonState['fileup']} neq 'hidden' and ${buttonState['fileup']} neq 'disable') or ${moduleTable} eq 'TMAIL_MAINCONTENT'or ${moduleTable} eq 'TMAIL_RECIPIENT'" >
        <div class="layui-btn layui-btn-normal layui-table-tools"
			 id="addbtn" th:if="${ableEdit}">
            <p class="dhicon dh-plus" style="color:white;font-size:14px"><span class="fa_span" style="color:#FFFFFF;">新增</span></p>
        </div>
        <div class="layui-btn layui-btn-danger layui-table-tools"
			 lay-event="delbtn" th:if="${ableEdit}">
            <p class="dhicon dh-remove" style="color:white;font-size:14px"><span class="fa_span" style="color:#FFFFFF;">删除</span></p>
        </div>
<!--        <div th:class="${buttonState['fileup']} eq 'enable'?'layui-btn layui-btn-normal layui-table-tools':'layui-btn layui-table-tools layui-btn-disabled'" 	-->
<!--			 th:id="${buttonState['fileup']} eq 'enable'?'addbtn':''" >-->
<!--            <p class="dhicon dh-plus" style="color:white;font-size:14px"><span class="fa_span" style="color:#FFFFFF;">新增</span></p>-->
<!--        </div>-->
<!--        <div th:class="${buttonState['filedelete']} eq 'enable'?'layui-btn layui-btn-danger layui-table-tools':'layui-btn layui-table-tools layui-btn-disabled'" 	-->
<!--			 th:lay-event="${buttonState['filedelete']} eq 'enable'?'delbtn':''">-->
<!--            <p class="dhicon dh-remove" style="color:white;font-size:14px"><span class="fa_span" style="color:#FFFFFF;">删除</span></p>-->
<!--        </div>-->
		<p class="max-warn">上传单文件最大 <span class="f_red">[[${maxFileSize}]]</span></p>
    </div>
</script>
<script type="text/html" id="barDh">
{{# if(d.fieldName != "Report"){ }}
    <a  class="layui-btn layui-btn-xs dhicon dh-search file-view"
	   lay-event="view" title="查看"></a>
{{# } }}
    <a class="layui-btn layui-btn-xs dhicon dh-download"
	   lay-event="download" title="下载"></a>
{{# if([[${ableEdit}]] == true){ }}
    <a class="layui-btn layui-btn-danger layui-btn-xs dhicon dh-remove"
	   lay-event="del" title="删除"></a>
{{# }else{}}
	
{{# } }}
{{# if(d.fieldName == "Report" && d.bHidden === true){ }}
<a class="layui-btn layui-bg-blue layui-btn-xs dhicon dh-eye"
   lay-event="show" title="显示"></a>
{{# } }}
{{# if(d.fieldName == "Report" && d.bHidden === false){ }}
<a class="layui-btn layui-btn-xs dhicon dh-eye-slash"
   lay-event="hide" title="隐藏"></a>
{{# } }}
 </script>
<!--<script type="text/html" id="barDh">-->
<!--{{# if(d.fieldName != "Report"){ }}-->
<!--    <a th:if="${buttonState['filedown']} neq 'hidden'" -->
<!-- 	   th:class="${buttonState['filedown']} eq 'enable'?'layui-btn layui-btn-xs dhicon dh-search file-view':'layui-btn layui-btn-xs dhicon dh-search layui-btn-disabled'" -->
<!--	   th:lay-event="${buttonState['filedown']} eq 'enable'?'view':''" title="查看"></a>-->
<!--{{# } }}-->
<!--    <a th:if="${buttonState['filedown']} neq 'hidden'" -->
<!-- 	   th:class="${buttonState['filedown']} eq 'enable'?'layui-btn layui-btn-xs dhicon dh-download':'layui-btn layui-btn-xs dhicon dh-download layui-btn-disabled'" -->
<!--	   th:lay-event="${buttonState['filedown']} eq 'enable'?'download':''" title="下载"></a>-->
<!--{{# if(d.createUser == "[[${session.mandatorUser.userId}]]" || [[${session.mandatorUser.userLevel}]] > 5){ }}-->
<!--    <a th:if="${buttonState['filedelete']} neq 'hidden'" -->
<!--	   class="layui-btn layui-btn-danger layui-btn-xs dhicon dh-remove" -->
<!--	   lay-event="del" title="删除"></a>-->
<!--{{# }else{}}-->
<!--	<a th:if="${buttonState['filedelete']} neq 'hidden'" -->
<!--	   th:class="${buttonState['filedelete']} eq 'enable'?'layui-btn layui-btn-danger layui-btn-xs dhicon dh-remove':'layui-btn layui-btn-xs dhicon dh-remove layui-btn-disabled'" -->
<!--	   th:lay-event="${buttonState['filedelete']} eq 'enable'?'del':''" title="删除"></a>-->
<!--{{# } }}-->
<!--{{# if(d.fieldName == "Report" && d.bHidden === true){ }}-->
<!--<a th:if="${buttonState['filedelete']} neq 'hidden'"-->
<!--   th:class="${buttonState['filedelete']} eq 'enable'?'layui-btn layui-bg-blue layui-btn-xs dhicon dh-eye':'layui-btn layui-btn-xs dhicon dh-eye layui-btn-disabled'"-->
<!--   th:lay-event="${buttonState['filedelete']} eq 'enable'?'show':''" title="显示"></a>-->
<!--{{# } }}-->
<!--{{# if(d.fieldName == "Report" && d.bHidden === false){ }}-->
<!--<a th:if="${buttonState['filedelete']} neq 'hidden'"-->
<!--   th:class="${buttonState['filedelete']} eq 'enable'?'layui-btn layui-btn-xs dhicon dh-eye-slash':'layui-btn layui-btn-xs dhicon dh-eye-slash layui-btn-disabled'"-->
<!--   th:lay-event="${buttonState['filedelete']} eq 'enable'?'hide':''" title="隐藏"></a>-->
<!--{{# } }}-->
<!-- </script>-->
<script>

	/*<![CDATA[*/
	var fileMark = "[[${fileMark}]]",
	fieldName = "[[${fieldName}]]",
	uploadUrl = "[[${uploadUrl}]]",
	fileViewUrl = "[[${fileViewUrl}]]",
	createUser = "[[${createUser}]]",
	moduleFile = "[[${moduleFile}]]",
	moduleTable = "[[${moduleTable}]]",
	vType = "[[${vType}]]", fileup = "[[${buttonState['fileup']}]]";
	var userId = "[[${session.mandatorUser.userId}]]";
	var userLevel = "[[${session.mandatorUser.userLevel}]]";
	console.log("1",fileup,moduleTable)
	/*]]>*/
	//获取父级页变量parent.fileBtnHide隐藏列表头部按钮
	if(fileup == "hidden" || parent.fileBtnHide){
		toolbar = '';
	}else{
		toolbar = '#toolbar';
	}
	var upload,fileCount = 0;
    layui.use(['table','layer','jquery','upload'], function () {
        var $ = layui.$,layer = layui.layer,table = layui.table,form = layui.form;
        upload = layui.upload;
        var tUrl = "/system/fileinfo/list?fileMark="+fileMark+"&fieldName="+fieldName;
        $.tablelist.init({
            elem: '#dhtable'
            ,url: tUrl
            ,cols: [[
                {type:'numbers', fixed: 'left'}
               	,{type: 'checkbox', fixed: 'left'}
                ,{field:'fileName', title: '文件名',align: 'left',width: 170}
                ,{field:'fileSize', title: '大小',align: 'left',width: 90}
                ,{field:'fileType', title: '类型',align: 'left',width: 70}
                ,{field:'createTime', title: '上传时间',align: 'left',width: 160}
                ,{field: 'filePath',title: 'filePath',hide:true}
                ,{field: 'guid',title: 'guid',hide:true}
                ,{title: '操作',align: 'center',width:170,templet:'#barDh'}
            ]
            ]
        	,toolbar: toolbar
            ,height:300
            ,width:760
            ,done: function(res, curr, count){
            	fileCount = count;
            	var state = "";
                for (var i in res.data) {
                    var item = res.data[i];
                    if (item.createUser != userId && userLevel < 6) {// 这里是判断需要禁用的条件（如：状态为0的）
                        // checkbox 根据条件设置不可选中
                        $('tr[data-index=' + i + '] input[type="checkbox"]').prop('disabled', true);
                        state = "1";// 隐藏表头全选判断状态
                        form.render();// 重新渲染一下
                    }
                }
             // 根据条件移除全选 checkbox
                if(state == "1")$('th[data-field=1] div').html('');
            }
        });
      	//头工具栏事件
        table.on('toolbar(ftable)', function(obj){
          var checkStatus = table.checkStatus(obj.config.id);
          switch(obj.event){
            case 'delbtn':
              var data = checkStatus.data,infoItems = [];
              $.each(checkStatus.data,function(i,v){
            	  infoItems.push({guid:v.guid,filePath:v.filePath});
              })
			  if(infoItems.length == 0){
				 layer.msg("请选中要删除的网络数据！");
				 return;
			  }
              $.operate.remove('确定要删除么？','/system/fileinfo/delete',{infoItems:JSON.stringify(infoItems)},function(result){
            	  if(result.code == '0'){
          			layer.closeAll();
                  	window.location.reload();
	              }else{
	              	layer.msg(result.msg);
	              }
              });
            break;
          };
        });
      	//监听工具条
        table.on('tool(ftable)', function (obj) {
            var data = obj.data;
            switch(obj.event){
            	case 'del':
            		$.operate.remove('确定要删除么？','/system/fileinfo/delete',{infoItems:JSON.stringify([{guid:data.guid,filePath:data.filePath}])},function(result){
                    	if(result.code == '0'){
                			layer.closeAll();
                        	window.location.reload();
                    	}else{
                    		layer.msg(result.msg);
                    	}
                    });
           		break;
				case 'hide':
					$.operate.remove('确定要隐藏么？','/system/fileinfo/editpost',{infoItems:JSON.stringify([{guid:data.guid,bHidden:true}])},function(result){
						if(result.code == '0'){
							layer.closeAll();
							window.location.reload();
						}else{
							layer.msg(result.msg);
						}
					});
					break;
				case 'show':
					$.operate.remove('确定要显示么？','/system/fileinfo/editpost',{infoItems:JSON.stringify([{guid:data.guid,bHidden:false}])},function(result){
						if(result.code == '0'){
							layer.closeAll();
							window.location.reload();
						}else{
							layer.msg(result.msg);
						}
					});
					break;
            	case 'download':
        			fetch(data.filePath).then(res => res.blob().then(blob => {
        			    let a = document.createElement('a');
        			    let url = window.URL.createObjectURL(blob);
        			    a.href = url;
        			    a.download = data.fileName + '.' + data.fileType;
        			    a.click();
        			    window.URL.revokeObjectURL(url);
        			}))
        			break;
            		// window.location.href = data.filePath;
           		break;
            	case 'view':
            		window.open(data.filePath);
           		break;
            }
        });
//         table.on('rowDouble(ftable)', function(obj){
//         	var data = obj.data;
//        		$.modal.open(data.fileName+'.'+data.fileType,uploadUrl+data.filePath,'700','450',function(index){
//        			layer.setTop(index);
//        		})
//        	});
      	var uploadData = {'moduleFile':moduleFile, 'vType':vType, 'fileMark':fileMark, 'fieldName':fieldName,'createUser':createUser}, ext = '';
      	if(vType == 'Report'){
      		ext = 'grf';
      	}
      	//上传附件、打印模板
      	$.uploadFile.common('#addbtn', uploadUrl, 'file', ext, uploadData);
    });

  	//回调
  	var callbackdata = function () {
   		return fileCount;
    }
</script>
</body>
</html>